package common.database;

import java.util.ArrayList;
import java.util.List;

import de.vogella.android.sqlite.first.Comment;
import de.vogella.android.sqlite.first.MySQLiteHelper;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class DBHandel {

	private SQLHelper dbHelper;
	private SQLiteDatabase db;

	public static final String DEBUG_TAG = "DBHandel";

	public DBHandel(Context context) {
		this.dbHelper = new SQLHelper(context);
	}

	public void open() throws SQLException {
		db = dbHelper.getWritableDatabase();
	}

	public void close() {
		db.close();
	}
	
	public void insertEmployee( Employee e) {
		
	}

	public Employee getEmployee(Employee e) {
		
		return e;
	}
	
	public void deleteEmployee( Employee e) {
		
	}
	
	public void insertSkill( Skill s) {
		
	}

	public Skill getSkill(Skill s) {
		return s;
	}
	
	public void deleteSkill( Skill s) {
		
	}

	public Skill getAllSkill() {
		return null;
	}

	public Employee getAllEmployee() {
//		List<Comment> comments = new ArrayList<Comment>();
//
//		Cursor cursor = db.query(MySQLiteHelper.TABLE_COMMENTS, allColumns,
//				null, null, null, null, null);
//
//		cursor.moveToFirst();
//		while (!cursor.isAfterLast()) {
//			Comment comment = cursorToComment(cursor);
//			comments.add(comment);
//			cursor.moveToNext();
//		}
//		// Make sure to close the cursor
//		cursor.close();
//		return comments;
		List<Employee> listEmp = new ArrayList<Employee>();
		
		Cursor c = db.query( SQLHelper.DB_NAME, SQLHelper.getAllColumnsEmployee(), null, null, null, null, null);
		return null;
	}

	public static class SQLHelper extends SQLiteOpenHelper {

		private static final String DB_NAME = "Employee_management.db";
		private static final int DB_VERSION = 1;

		public SQLHelper(Context context) {
			super(context, DB_NAME, null, DB_VERSION);
		}

		@Override
		public void onCreate(SQLiteDatabase db) {
			String sql1 = Common.createSQLForInstallTable(Employee
					.getInstance());
			String sql2 = Common.createSQLForInstallTable(Skill.getInstance());
			db.execSQL(sql1);
			db.execSQL(sql2);
		}

		@Override
		public void onUpgrade(SQLiteDatabase db, int odlVersion, int newVersion) {
			Log.d(DEBUG_TAG, "Upgrate database");
			String query1 = Common.createSQLForRemoveTable(Employee
					.getInstance());
			String query2 = Common.createSQLForRemoveTable(Skill.getInstance());
			db.execSQL(query1);
			db.execSQL(query2);

			this.onCreate(db);
		}
		
		public static String[] getAllColumnsEmployee() {
			List<String> list = new ArrayList<String>();
			for (int i = 0; i < Employee.COLUMNS.length; i++) {
				list.add( Employee.COLUMNS[i][0]);
			}
			Log.d(DEBUG_TAG, list.toString());
			return list.toArray(null);
		}
		
		public static String getAllColumnsSkill() {
			List<String> list = new ArrayList<String>();
			for (int i = 0; i < Skill.COLUMNS.length; i++) {
				list.add( Employee.COLUMNS[i][0]);
			}
			Log.d(DEBUG_TAG, list.toString());
			return list.toString();
		}
	}
}
